package com.android.basiclib.base;

import android.app.Activity;


import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.android.basiclib.R;
import com.android.basiclib.cache.SPUtils;
import com.android.basiclib.entity.LoadAction;
import com.android.basiclib.uitls.CheckUtil;
import com.android.basiclib.uitls.CommUtils;
import com.android.basiclib.uitls.NetWorkUtil;

import java.util.ArrayList;


import com.android.basiclib.uitls.ToastUtils;


/**
 * MVVM的ViewModel基类，自己也实现了Disposable，可以选择把网络抽离到Repository中
 * 基类实现了两个构造方法，空参为默认的构造，不会有activity实例和bundle
 * 如果想传递Activity和Bundle的话，可以由子类ViewModel实现双参构造方法,只要实现就有，无需Activity再去实现，Activity只需要重写setBundle设置进ViewModel
 */
public class BaseViewModel extends ViewModel implements IBaseViewModelAction {

    //数据仓库->可以到数据仓库中实现数据的获取,也可以就在ViewModel中实现
    public BaseRepository mRepository;
    //每一个页面的加载的状态的LiveData
    public MutableLiveData<LoadAction> mLoadActionLiveData = new MutableLiveData<>();
    //Activity对象,由构造方法传递进来
    public Activity mActivity;

    /**
     * 默认没有实现双参构造方法，那么会走到空参构造
     */
    public BaseViewModel() {
        mRepository = createRepository(mLoadActionLiveData);
    }

    /**
     * 需要在子类实现双参数的构造方法才能正常的反射构造,
     * 如果子类想拿到Activity的引用,需要实现带Activity参数的构造方法
     */
    public BaseViewModel(Activity activity) {
        mRepository = createRepository(mLoadActionLiveData);
        mActivity = activity;
    }

    //初始化Repository,必须传递状态LiveData过去
    protected BaseRepository createRepository(MutableLiveData<LoadAction> stateLiveData) {
        return null;
    }


    /**
     * 设置并发射加载状态
     */
    public void setStateLiveData(LoadAction loadState) {
        if (mLoadActionLiveData != null) {
            mLoadActionLiveData.postValue(loadState);
        }
    }


    @Override
    public void loadStartLoading() {
        loadStartLoading(null);
    }

    @Override
    public void loadStartLoading(String message) {
        mLoadActionLiveData.postValue(new LoadAction(LoadAction.STATE_LOADING, message));
    }

    @Override
    public void loadSuccess() {
        mLoadActionLiveData.postValue(new LoadAction(LoadAction.STATE_SUCCESS));
    }

    @Override
    public void loadError(String message) {
        mLoadActionLiveData.postValue(new LoadAction(LoadAction.STATE_ERROR, message));
    }

    @Override
    public void loadNoData() {
        mLoadActionLiveData.postValue(new LoadAction(LoadAction.STATE_NO_DATA));
    }

    @Override
    public MutableLiveData<LoadAction> getActionLiveData() {
        return mLoadActionLiveData;
    }

    // ==================== 业务逻辑相关 ========================

    private String getToken() {
        String token = SPUtils.getInstance(CommUtils.getContext()).getString("user_token", "");
        if (CheckUtil.isEmpty(token)) {
            return null;
        } else {
            return token = "bearer" + token;
        }
    }

    /**
     * 检查当前的网络和token状态
     */
    public String checkTokenAndStutus() {
        if (NetWorkUtil.isConnected(CommUtils.getContext()) && NetWorkUtil.isAvailable(CommUtils.getContext())) {
            String token = getToken();
            return token;
        } else {
            ToastUtils.makeText(CommUtils.getContext(), "Network Error.");
            return null;
        }
    }

    public void cleanTask() {
        //清除网络异步任务
    }
}
